package cn.edu.xmu.oomall.customer.dao.openfegin;


import cn.edu.xmu.javaee.core.model.InternalReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.javaee.core.util.CloneFactory;
import cn.edu.xmu.oomall.customer.dao.CouponDao;
import cn.edu.xmu.oomall.customer.dao.bo.CouponAct;
import cn.edu.xmu.oomall.customer.mapper.mongo.CouponActPoMapper;
import cn.edu.xmu.oomall.customer.mapper.openfegin.CouponActMapper;
import cn.edu.xmu.oomall.customer.mapper.openfegin.po.CouponActPo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

@Repository
@RequiredArgsConstructor
@Slf4j
public class CouponActDao {

    private final CouponActMapper couponActMapper;

    private final CouponActPoMapper couponActPoMapper;

    private final RedissonClient redissonClient;

    private final CouponDao couponDao;

    private CouponAct build(CouponAct bo) {
        bo.setCouponActDao(this);
        bo.setCouponDao(couponDao);
        bo.setRedissonClient(redissonClient);
        return bo;
    }


    public CouponAct findById(Long actId) {
        InternalReturnObject<CouponAct> ret = couponActMapper.findCouponActById(actId);
        return this.build(ret.getData());
    }

    public void save(CouponAct bo, UserDto user) {
        bo.setModifier(user);
        bo.setGmtModified(LocalDateTime.now());
        log.debug("update: couponAct={}", bo);
        CouponActPo po = CloneFactory.copy(new CouponActPo(), bo);
        couponActPoMapper.save(po);
    }

}
